package com.ttxs.uaa.api.common;


import com.ttxs.common.core.vo.R;
import com.ttxs.uaa.api.BaseApi;
import com.ttxs.common.core.constant.Api;
import com.ttxs.uaa.service.ISysUserService;
import com.ttxs.uaa.vo.UserPasswordVO;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(Api.Common.Person.PREFIX)
@RequiredArgsConstructor
public class PersonApi extends BaseApi {
    private final ISysUserService sysUserService;

    @PutMapping(Api.Common.Person.CHANGE_PASSWORD)
    public R changePassword(@RequestBody UserPasswordVO userPasswordVO) {
        if(!StringUtils.hasText(userPasswordVO.getNewPassword())) {
            return R.error("密码不能为空或空白字符");
        }
        if(userPasswordVO.getNewPassword().equals(userPasswordVO.getOldPassword())) {
            return R.error("新密码不能和旧密码一样");
        }
        if(!userPasswordVO.getNewPassword().equals(userPasswordVO.getConfirmPassword())) {
            return R.error("新密码和确认密码不一致");
        }
        this.sysUserService.changePassword(getUserId(), getPlatformId(), userPasswordVO.getNewPassword(), userPasswordVO.getOldPassword());
        return R.ok();
    }
}
